/**
 * 
 */
package com.my.dao.impl;

import java.math.BigInteger;
import java.util.List;

import com.my.bean.Role;
import com.my.dao.IBaseDAO;
import com.my.dao.RoleDAO;

/**
 * @author Administrator
 * 
 */
public class RoleDAOImpl implements RoleDAO {

	private IBaseDAO<Role> basedao;

	public void setBasedao(IBaseDAO<Role> basedao) {
		this.basedao = basedao;
	}

	@Override
	public boolean isExist(String roleName) {
		if (roleName.isEmpty()) {
			return false;
		}
		String hql = "from Role as r where r.roleName=?";
		return basedao.isExist(hql, roleName);
	}

	@Override
	public boolean addRole(Role role) throws Exception {
		if (this.checkRole(role)) {
			return basedao.add(role);
		}
		return false;
	}

	@Override
	public boolean editRole(Role role) throws Exception {
		if (this.checkRole(role)) {
			return basedao.edit(role);
		}
		return false;

	}

	@Override
	public Role getRoleById(Class<Role> c, Integer id) throws Exception {
		return basedao.getById(c, id);
	}

	@Override
	public boolean removeRole(Role role) throws Exception {
		if (role != null && role.getId() != null) {
			role = basedao.loadById(Role.class, role.getId());
			return basedao.remove(role);
		}
		return false;
	}

	@Override
	public BigInteger getTotalRoleRecords() throws Exception {
		String sql = "select count(role_id) from info_role where flag=0";
		return basedao.getTotalRecords(sql);
	}

	@Override
	public List<Role> getRoleList() throws Exception {
		String hql = "from Role";
		return basedao.getList(hql);
	}

	@Override
	public List<Role> getAllRoleByPage(int pageindex, int pagesize)
			throws Exception {
		String hql = "from Role";
		return basedao.getAllEntityByPage(hql, pageindex, pagesize);
	}

	boolean checkRole(final Role role) {
		if (role == null || role.getRoleName().isEmpty()) {
			return false;
		} else {
			return true;
		}
	}

	@Override
	public BigInteger searchTotalRoleRecords(Role role) throws Exception {
		String sql = "select count(role_id) from info_role where role_name like ? and flag=0";
		String item = role.getRoleName() + "%";
		return basedao.getTotalRecords(sql, item);
	}

	@Override
	public List<Role> searchAllRoleByPage(int pageindex, int pagesize, Role role)
			throws Exception {
		String hql = "from Role as r where r.roleName like ?";
		String item = role.getRoleName() + "%";
		return basedao.getAllEntityByPage(hql, pageindex, pagesize, item);
	}
}
